package com.psfd.demo.controller;

import com.psfd.demo.bean.Singer;
import com.psfd.demo.bean.Song;
import com.psfd.demo.service.SongService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class SongController {

    @Resource
    private SongService songService;

    @RequestMapping("/song")
    public ModelAndView query(){
        List<Song> list = songService.list();
        for (Song song : list) {
            System.out.println(song);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("songList",songService.list());
        modelAndView.setViewName("songlist");
        return modelAndView;
    }

    @RequestMapping("/deletesong")
    public ModelAndView delete(Integer id){
        songService.removeById(id);
        return query();
    }

    @RequestMapping("/addsong")
    public ModelAndView add(Song song){
        songService.save(song);
        return query();
    }

    @RequestMapping("/updatesonghtml")
    public ModelAndView updatehtml(Integer soId){
        ModelAndView view = new ModelAndView();
        view.addObject("soId",soId);
        view.setViewName("songupdate");
        return view;
    }

    @RequestMapping("/updatesong")
    public ModelAndView update(Song song){
        songService.updateById(song);
        return query();
    }

}
